﻿/**
 * http://vkontakte.ru/app1850321_355679
 * @author Артём Григорьев
 * @author Иннокентий Шувалов
 */

package ;

import api.DAO;
import flash.net.URLRequest;

import flash.display.Sprite;
import flash.Lib;
import flash.events.Event;

import gui.hug.HUGSprite;
import gui.hug.HUGTabbedPanel;
import gui.hug.Tab;

import tab.CategoriesTab;
import tab.MainTab;
import tab.FavoritesTab;

import loader.Loader;

class Application extends Sprite
{

	private var viewerId 			: Int;
	private var tabbedPanel 		: HUGTabbedPanel;
	private static var wrapper		: Dynamic;
	private static var params	    : Dynamic;
	private static var application  : Application;
	
	public function new() 
	{
		super();
		Lib.current.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
	}
	
	public function onAddedToStage(e: Dynamic) {
		main();
	}
	
	public function main() 
	{
		Lib.current.addChild(this);
		Lib.current.addChild(getInstance());
		
		tabbedPanel = new HUGTabbedPanel(607);
		this.addChild(tabbedPanel);
		
		params = stage.loaderInfo.parameters;
		viewerId = getViewerId();
		Loader.createInstance();
		
		this.addMainTab(viewerId);
		
		tabbedPanel.addTab(new CategoriesTab(tabbedPanel, viewerId));
		tabbedPanel.addTab(new FavoritesTab(tabbedPanel, viewerId));
		tabbedPanel.setTab(1);
		wrapper = this.parent.parent.parent;
		showInstallBox();
		ResizeWindows(650, 700);
		SetTitle("Уникальный новостной агрегатор");
	}
	
	public function addMainTab(uid : Int)
	{
		tabbedPanel.addTab(new MainTab(tabbedPanel, uid));
	}
	
	public function addTab(sprite : HUGSprite, name : String, type:Int)
	{
		var tab = new Tab(sprite, name, type);
		tabbedPanel.addTab(tab);
	}

	
//	public function onKeyDown ( e : KeyboardEvent ) {
	/*	switch (e.keyCode) {
			case Keyboard.ENTER : {
			
				// Кекс, разберись с getItem!!!!! (Тёма)
				addTab(new HUGSprite(), newsList.getItem().getName());
			}
			case Keyboard.F5 : {
				tabbedPanel.draw();
			}
		} */
	//}
	
	public static function getInstance() {
		if (application == null) {
			application = new Application();
		} return application; 
	}
	
	public static function showInviteBox() {
		wrapper.external.showInviteBox();
	}
		
	public static function getViewerId() : Int { 
		return Reflect.field(params, "viewer_id");
	}
	
	public static function getUserId() : Int {
		return Reflect.field(params, "user_id");
	}
	
	public function showInstallBox() {
		wrapper.external.showInstallBox();
	}
	
	public static function getAuthKey() : String {	
		return Reflect.field(params, "auth_key");
	}
	
	private function ResizeWindows(x : Int, y : Int) {	
		wrapper.external.resizeWindow(x, y);
	}
	
	private function SetTitle(title : String){
		wrapper.external.setTitle(title);
	}
}
	